package com.infosys.cabplanning.service.cab;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.infosys.cabplanning.common.domain.Cab;
import com.infosys.cabplanning.common.domain.CabDto;
import com.infosys.cabplanning.common.domain.CabType;
import com.infosys.cabplanning.helper.JsonFormatter;
import com.infosys.cabplanningdao.CabPlanningDAOService;

@Controller
public class CabController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @Autowired
    public CabPlanningDAOService daoService;
    
    @Autowired
    JsonFormatter jsonFormatter;
    
    @RequestMapping(value="cab", method = RequestMethod.POST,produces="application/json",consumes="application/json")
    public @ResponseBody List<CabDto> cab(@RequestBody CabDto cab) throws Exception {
    		System.out.println("Entered CabDtos method");
    		List<Cab> cabList = daoService.findCabs(convertTOCab(cab));
    		List<CabDto> cabDTOList = getCabDTOList(cabList);
//    		System.out.println("Cab DTO Reg NO : "+cabDTOList.get(0).getRegistrationNo());
		 return cabDTOList; 
    }
    
    private List<CabDto> getCabDTOList(List<Cab> cabList) {
    	System.out.println("Creating CabDto List");
    	List<CabDto> cabDTOList = new ArrayList<CabDto>();
    	if (cabList != null && !cabList.isEmpty()){
    		for(Cab cab : cabList) {
    			cabDTOList.add(convertTOCabDTO(cab));
    		}
    	}
    	System.out.println("CabDto Size : "+cabDTOList.size());
    	return cabDTOList;
	}
    
    private List<Cab> getCabList(List<CabDto> cabDtoList) {
    	System.out.println("Creating Cab List");
    	List<Cab> cabList = new ArrayList<Cab>();
    	if (cabDtoList != null && !cabDtoList.isEmpty()){
    		for(CabDto cabDto : cabDtoList) {
    			cabList.add(convertTOCab(cabDto));
    		}
    	}
    	System.out.println("CabDto Size : "+cabList.size());
    	return cabList;
	}

	private Cab convertTOCab(CabDto cabDTO) {
    	Cab cab = new Cab();
    	cab.setCabType(new CabType());
    	cab.getCabType().setCabTypeID(cabDTO.getCabTypeID());
    	cab.getCabType().setName(cabDTO.getName());
    	cab.getCabType().setCapacity(cabDTO.getCapacity());
    	cab.setRegistrationNo(cabDTO.getRegistrationNo());
		return cab;
	}
    
    private CabDto convertTOCabDTO(Cab cab) {
    	CabDto cabDTO = new CabDto();
    	cabDTO.setCabTypeID(cab.getCabType().getCabTypeID());
    	cabDTO.setName(cab.getCabType().getName());
    	cabDTO.setCapacity(cab.getCabType().getCapacity());
    	cabDTO.setRegistrationNo(cab.getRegistrationNo());
		return cabDTO;
	}

	@RequestMapping(value="savecab", method = RequestMethod.POST ,produces="application/json",consumes="application/json")
    public @ResponseBody List<CabDto> savecab(@RequestBody String json) throws Exception {
    		System.out.println("Entered savecab method");
    		List<Object> list = jsonFormatter.convertToList(json, new CabDto());
    		List<CabDto> cabDTOList = new ArrayList<CabDto>();
    		List<Cab> cabList = new ArrayList<Cab>();
    		for(Object obj : list) {
    			cabDTOList.add((CabDto)obj);
    		}
    		if (!cabDTOList.isEmpty()) {
    			cabList = getCabList(cabDTOList);
    		}
    		daoService.saveCab(cabList);
    		return cabDTOList;
    }
}